struct Point {
    x: f64,
    y: f64,
}

impl std::fmt::Debug for Point {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "({}, {})", self.x, self.y)
    }
}

// 关联函数
impl Point {
    fn show()->Self{
        Point{x:1.0,y:2.0}
    }
}

#[derive(Debug)]
struct Rectangle {
    top_left: Point,
    bottom_right: Point,
}

impl Point {
    fn distance(&self, other: &Point) -> f64 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;
        (dx.powi(2) + dy.powi(2)).sqrt()
    }
}

fn distance(p1: &Point, p2: &Point) -> f64 {
    let dx = p1.x - p2.x;
    let dy = p1.y - p2.y;
    (dx.powi(2) + dy.powi(2)).sqrt()
}

struct Color(u8, u8, u8);

impl Color {
    fn new(r: u8, g: u8, b: u8) -> Self {
        Self(r, g, b)
    }
}

// 关联函数
impl std::fmt::Display for Color {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "({} {} {})", self.0, self.1, self.2)
    }
}

fn main() {
    let p1 = Point { x: 0.0, y: 0.0 };
    let p2 = Point { x: 3.0, y: 4.0 };
    println!("Distance between p1 and p2 is {}", p1.distance(&p2));
    let d = distance(&p1, &p2);
    println!("Distance between p1 and p2 is {}", d);

    let c1 = Color::new(255, 0, 0);
    println!("Color is {}{}{}", c1.0, c1.1, c1.2);
    println!("Color is {}", c1);

    let r1 = Rectangle {
        top_left: Point { x: 0.0, y: 0.0 },
        bottom_right: Point { x: 10.0, y: 10.0 },
    };
    println!("Rectangle is {:?}", r1);

    let p3 = Point::show();
    println!("Point is {:?}", p3);
}